<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500"
    :before-close="() => cancel()"
  >
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="所属类型" prop="dictTypeKey">
        <el-select
          v-if="typeList && typeList.length"
          v-model="ruleForm.dictTypeKey"
          placeholder="请选择"
          clearable
          style="width: 180px"
        >
          <el-option
            v-for="item in typeList"
            :key="item.dictType"
            :label="item.dictName"
            :value="item.dictType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字典名称" prop="dictLabel">
        <el-input v-model="ruleForm.dictLabel" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典选项编码" prop="dictValue">
        <el-input v-model="ruleForm.dictValue" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典描述" prop="remark">
        <el-input
          v-model="ruleForm.remark"
          placeholder="请输入字典名称"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 10 }"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-switch
          v-model="ruleForm.status"
          active-value="0"
          inactive-value="1"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="() => cancel()">取 消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          保 存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { dictdAdDictData, dictUpdateDictData } from '@/api/system';

const { item, type, typeList } = defineProps(['item', 'type', 'typeList']);
const emit = defineEmits(['cancel']);

onMounted(() => {
  console.log(item);
  console.log(type);
  if (type === 'edit') {
    title.value = '编辑字典选项';
    ruleForm.dictCode = item.dictCode;
    ruleForm.dictTypeKey = item.dictType;
    ruleForm.dictLabel = item.dictLabel;
    ruleForm.dictValue = item.dictValue;
    ruleForm.remark = item.remark;
    ruleForm.status = item.status;
  }
});

const dialogVisible = ref(true);
const title = ref('新增字典选项');
const ruleFormRef = ref();

const ruleForm = reactive({
  dictTypeKey: '',
  dictLabel: '',
  dictValue: '',
  remark: '',
  status: '0',
});
const rules = reactive({
  dictTypeKey: [{ required: true, message: '请选择字典类型', trigger: 'blur' }],
  dictLabel: { required: true, message: '请输入字典名称', trigger: 'blur' },
  dictValue: { required: true, message: '字典选项编码', trigger: 'blur' },
});
const submitForm = async (ruleFormRef) => {
  await ruleFormRef.validate((valid) => {
    if (valid) {
      if (type === 'add') {
        dictdAdDictData(ruleForm).then(() => {
          cancel(true);
        });
      } else {
        dictUpdateDictData(ruleForm).then(() => {
          cancel(true);
        });
      }
    }
  });
};
const cancel = (flag = false) => {
  emit('cancel', flag);
};
</script>

<style lang="less" scoped>
.search-card,
.table-card {
  margin-bottom: 20px;

  .el-card__header {
    padding: 15px 20px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header-buttons {
    display: flex;
    gap: 10px;
  }
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.text-muted {
  font-size: 13px;
  color: #909399;
  margin-top: 5px;
}
</style>
